package com.ruoyi.energy.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.energy.domain.NyCableAccount;
import com.ruoyi.energy.domain.vo.NyCableAccountVO;
import com.ruoyi.energy.service.INyCableAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 电缆台账Controller
 *
 * @author cjie
 * @date 2021-09-25
 */
@Api(tags = " 电缆台账")
@RestController
@RequestMapping("/nyCableAccount")
public class NyCableAccountController extends BaseController {
    @Autowired
    private INyCableAccountService nyCableAccountService;

    /**
     * 查询电缆台账列表
     */
    @ApiOperation(value="查询电缆台账列表", notes="查询电缆台账列表")
    @PreAuthorize(hasPermi = "enery:nyCableAccount:list")
    @GetMapping("/list")
    public TableDataInfo list(NyCableAccount nyCableAccount) {
        startPage();
        List<NyCableAccount> list = nyCableAccountService.selectNyCableAccountList(nyCableAccount);
        return getDataTable(list);
    }

    /**
     * 导出电缆台账列表
     */
    @ApiOperation(value="导出电缆台账列表", notes="导出电缆台账列表")
    @PreAuthorize(hasPermi = "enery:nyCableAccount:export")
    @Log(title = "电缆台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NyCableAccount nyCableAccount) throws IOException {
        List<NyCableAccount> list = nyCableAccountService.selectNyCableAccountList(nyCableAccount);
        ExcelUtil<NyCableAccount> util = new ExcelUtil<NyCableAccount>(NyCableAccount.class);
        util.exportExcel(response, list, "电缆台账数据");
    }
    /**
     * 获取电缆台账详细信息
     */
    @ApiOperation(value="获取电缆台账详细信息", notes="获取电缆台账详细信息")
    @PreAuthorize(hasPermi = "enery:nyCableAccount:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(nyCableAccountService.selectNyCableAccountById(id));
    }

    /**
     * 新增电缆台账
     */
    @ApiOperation(value="新增电缆台账", notes="新增电缆台账")
    @PreAuthorize(hasPermi = "enery:nyCableAccount:add")
    @Log(title = "电缆台账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NyCableAccount nyCableAccount) {
        return toAjax(nyCableAccountService.insertNyCableAccount(nyCableAccount));
    }

    /**
     * 修改电缆台账
     */
    @ApiOperation(value="修改电缆台账", notes="修改电缆台账")
    @PreAuthorize(hasPermi = "enery:nyCableAccount:edit")
    @Log(title = "电缆台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NyCableAccount nyCableAccount) {
        return toAjax(nyCableAccountService.updateNyCableAccount(nyCableAccount));
    }

    /**
     * 删除电缆台账
     */
    @ApiOperation(value="删除电缆台账", notes="删除电缆台账")
    @ApiImplicitParam(name = "ids", value = "电缆台账ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "enery:nyCableAccount:remove")
    @Log(title = "电缆台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(nyCableAccountService.deleteNyCableAccountByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<NyCableAccountVO> util = new ExcelUtil<NyCableAccountVO>(NyCableAccountVO.class);
        util.importTemplateExcel(response, "线缆数据");
    }

    /**
     * 导入抄表记录列表,
     */
    @PreAuthorize(hasPermi = "park:buiRoom:edit")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<NyCableAccountVO> util = new ExcelUtil<NyCableAccountVO>(NyCableAccountVO.class);
        List<NyCableAccountVO> buiRoomVOList = util.importExcel(file.getInputStream());
        //批量插入数据
        String message = nyCableAccountService.importData(buiRoomVOList);
        return AjaxResult.success(message);

    }
}
